<template>
  <div class="background-preview-page">
    <!-- 动态背景 -->
    <component
      v-if="backgroundConfig"
      :is="getBackgroundComponent(backgroundConfig.type)"
      :config="backgroundConfig.config"
      :key="`preview-${backgroundConfig.id}`"
    />

    <!-- 信息面板 -->
    <div class="info-panel">
      <div class="panel-content">
        <h1>{{ backgroundConfig?.name }}</h1>
        <p class="description">{{ backgroundConfig?.description }}</p>
        <div class="meta-info">
          <el-tag :type="getCategoryType(backgroundConfig?.category)" size="large">
            {{ getCategoryName(backgroundConfig?.category) }}
          </el-tag>
        </div>
      </div>
    </div>

    <!-- 操作栏 -->
    <div class="action-bar">
      <el-button @click="closePreview">
        <el-icon><Close /></el-icon>
        关闭预览
      </el-button>
      <el-button type="primary" @click="applyBackground">
        <el-icon><Check /></el-icon>
        应用此背景
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, markRaw } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Close, Check } from '@element-plus/icons-vue'

// 动态背景组件
import ParticleBackground from './backgrounds/ParticleBackground.vue'
import GradientBackground from './backgrounds/GradientBackground.vue'
import WaveBackground from './backgrounds/WaveBackground.vue'
import StarBackground from './backgrounds/StarBackground.vue'
import GeometryBackground from './backgrounds/GeometryBackground.vue'
import AbstractBackground from './backgrounds/AbstractBackground.vue'
import MatrixBackground from './backgrounds/MatrixBackground.vue'
import BubbleBackground from './backgrounds/BubbleBackground.vue'
import SnowBackground from './backgrounds/SnowBackground.vue'
import FireworksBackground from './backgrounds/FireworksBackground.vue'
import ScanBackground from './backgrounds/ScanBackground.vue'
import NeonBackground from './backgrounds/NeonBackground.vue'

const route = useRoute()
const router = useRouter()
const backgroundConfig = ref(null)

// 获取背景组件
const getBackgroundComponent = (type) => {
  const components = {
    particle: markRaw(ParticleBackground),
    gradient: markRaw(GradientBackground),
    wave: markRaw(WaveBackground),
    star: markRaw(StarBackground),
    geometry: markRaw(GeometryBackground),
    abstract: markRaw(AbstractBackground),
    matrix: markRaw(MatrixBackground),
    bubble: markRaw(BubbleBackground),
    snow: markRaw(SnowBackground),
    fireworks: markRaw(FireworksBackground),
    scan: markRaw(ScanBackground),
    neon: markRaw(NeonBackground)
  }
  return components[type] || components.gradient
}

// 获取分类名称
const getCategoryName = (category) => {
  const names = {
    particle: '粒子',
    gradient: '渐变',
    wave: '波浪',
    star: '星空',
    geometry: '几何',
    abstract: '抽象',
    matrix: '矩阵',
    bubble: '气泡',
    snow: '雪花'
  }
  return names[category] || category
}

// 获取分类类型
const getCategoryType = (category) => {
  const types = {
    particle: 'primary',
    gradient: 'success',
    wave: 'info',
    star: 'warning',
    geometry: 'danger',
    abstract: '',
    matrix: 'success',
    bubble: 'primary',
    snow: 'info'
  }
  return types[category] || ''
}

// 关闭预览
const closePreview = () => {
  window.close()
  // 如果无法关闭窗口（不是window.open打开的），返回上一页
  setTimeout(() => {
    router.back()
  }, 100)
}

// 应用背景
const applyBackground = () => {
  if (backgroundConfig.value) {
    localStorage.setItem('currentBackground', JSON.stringify(backgroundConfig.value))
    ElMessage.success(`已应用背景：${backgroundConfig.value.name}`)

    // 通知父窗口
    if (window.opener) {
      window.opener.postMessage({
        type: 'applyBackground',
        data: backgroundConfig.value
      }, '*')
    }

    setTimeout(() => {
      closePreview()
    }, 1000)
  }
}

// 从URL参数加载背景配置
onMounted(() => {
  const configStr = route.query.config
  if (configStr) {
    try {
      backgroundConfig.value = JSON.parse(decodeURIComponent(configStr))
    } catch (e) {
      console.error('解析背景配置失败:', e)
      ElMessage.error('加载背景配置失败')
    }
  }
})
</script>

<style scoped>
.background-preview-page {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #000;
}

.background-preview-page > *:first-child {
  position: absolute !important;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

/* 信息面板 */
.info-panel {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  pointer-events: none;
}

.panel-content {
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(20px);
  padding: 24px 32px;
  border-radius: 16px;
  text-align: center;
  color: white;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.1);
  pointer-events: auto;
}

.panel-content h1 {
  margin: 0 0 12px 0;
  font-size: 32px;
  font-weight: 600;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

.description {
  margin: 0 0 16px 0;
  font-size: 16px;
  opacity: 0.9;
  max-width: 600px;
}

.meta-info {
  display: flex;
  justify-content: center;
  gap: 12px;
}

/* 操作栏 */
.action-bar {
  position: fixed;
  bottom: 50px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  gap: 12px;
  padding: 12px 20px;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(20px);
  border-radius: 50px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.action-bar .el-button {
  padding: 10px 24px;
  font-size: 14px;
  font-weight: 500;
}

/* 响应式 */
@media (max-width: 768px) {
  .panel-content h1 {
    font-size: 24px;
  }

  .description {
    font-size: 14px;
  }

  .action-bar {
    flex-direction: column;
    bottom: 10px;
  }
}
</style>

